define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.core.Deserializer();
    Window.container = Window.container || {};
    dsBridge.register('create_ClusterLayer', function (sourceId, layerId, name, distance, textFillColor, fillColor, fillStrokeColor, fillStrokeWidth, clusterLevel, clusterScatterSize, radiusScale) {
        try {
            var deserializer = Window.deserializer;
            console.log("color:" + textFillColor);
            textFillColor = deserializer.createJsInstance(JSON.parse(textFillColor));
            fillColor = deserializer.createJsInstance(JSON.parse(fillColor));
            fillStrokeColor = deserializer.createJsInstance(JSON.parse(fillStrokeColor));
            var layer = new egis.carto.ClusterLayer({
                "id": layerId,
                "name": name,
                "distance": distance,
                "textFillColor": textFillColor.toArray(),
                "fillColor": fillColor.toArray(),
                "fillStrokeColor": fillStrokeColor.toArray(),
                "fillStrokeWidth": fillStrokeWidth,
                "clusterLevel":clusterLevel,
                "clusterScatterSize":clusterScatterSize,
                "radiusScale":radiusScale
            });
            Window.container[sourceId] = layer;
        }
        catch (e) {
            alert(e);
        }
    })

    dsBridge.register('ClusterLayer_add', function (sourceId, elementId, content) {
        try {
            var clusterLayer = Window.container[sourceId];
            var target = Window.container[elementId];
            var element = Window.deserializer.createJsInstance(JSON.parse(content));
            target.geometry = element.geometry;
            target.symbol = element.symbol;
            target.tag = element.tag;
            clusterLayer.add(target);
        }
        catch (e) {
            alert(e);
        }
    })

    dsBridge.register('ClusterLayer_addElements', function (sourceId, content) {
        try {
            var clusterLayer = Window.container[sourceId];
            console.log('elements:' + content)
            var elements = Window.deserializer.createJsInstance(JSON.parse(content));
            clusterLayer.addElements(elements);
        }
        catch (e) {
            alert(e);
        }
    })

    dsBridge.register('ClusterLayer_remove', function (sourceId, elementId) {
            var source = Window.container[sourceId];
            var element = Window.container[elementId];
            source.remove(element);
        })

    dsBridge.register('ClusterLayer_hitTest', function (sourceId, screenX, screenY) {
        try {
            var source = Window.container[sourceId];
            var graphic = source.hitTest(screenX, screenY);
            if (!!graphic && !!graphic.element) {
                var json = JSON.stringify(graphic.element);
                return json;
            }
        }
        catch (e) {
            alert(e);
        }
    })
    dsBridge.register('ClusterLayer_hitTestList', function (sourceId, screenX, screenY) {
        try {
            var source = Window.container[sourceId];
            var graphicSet = source.hitTestList(screenX, screenY);
            if (!!graphicSet) {
                var elementArray=[];
                if(!!graphicSet.graphiclist){
                    graphicSet.graphiclist.forEach((item,index)=>{
                        elementArray.push(item.element);
                    })
                    var json = JSON.stringify(elementArray);
                    return json;
                }
            }
        }
        catch (e) {
            alert(e);
        }
    })
    dsBridge.register('ClusterLayer_hitTestListScatter', function (sourceId, screenX, screenY) {
        try {
            var source = Window.container[sourceId];
            var graphicSet = source.hitTestListScatter(screenX, screenY);
            if (!!graphicSet) {
                var elementArray=[];
                if(!!graphicSet.graphiclist){
                    graphicSet.graphiclist.forEach((item,index)=>{
                        elementArray.push(item.element);
                    })
                    var json = JSON.stringify(elementArray);
                    return json;
                }
            }
        }
        catch (e) {
            alert(e);
        }
    })
})